home *** CD-ROM | disk | FTP | other *** search
- /*
- IC Text Whats.c
-
- */
-
- #include <Scrap.h>
-
- #include "IC Globals.h"
-
- #include "IC Window Globals.h"
- #include "IC Types.h"
- #include "IC API.h"
- #include "IC Globals.h"
- #include "IC Misc Subs.h"
- #include "IC Subs.h"
- #include "IC Text.h"
- #include "IC Dialogs.h"
- #include "IC Document.h"
- #include "IC Text Whats.h"
-
- pascal void DrawTextProc(DialogPtr window,short item){
- Rect r;
- short savefont,savemode,savesize;
- Style saveface;
-
- savefont=window->txFont;
- saveface=window->txFace;
- savemode=window->txMode;
- savesize=window->txSize;
-
- TextDraw(WindowInfo[GetWindowType(window)].items[item]->data);
-
- GetDItemRect(window,item,&r);
- InsetRect(&r,-3,-3);
- PenNormal();
- FrameRect(&r);
-
- TextFont(savefont);
- TextFace(saveface);
- TextMode(savemode);
- TextSize(savesize);
- }
-
- Boolean HaveTextSelection(WindowType wt){
- long start_sel,end_sel;
- Boolean ret=false;
-
- if (GetSelectedItem(wt)>0){
- TextGetSelect(WindowInfo[wt].items[GetSelectedItem(wt)]->data,&start_sel,&end_sel);
- ret=(start_sel!=end_sel);
- }
-
- return ret;
- }
-
- void AdjustTextMenu(WindowType wt){
- Boolean enable_cut=false,enable_paste=false,enable_select_all=false;
- Boolean enable_undo=false;
- long offset;
- MenuHandle mh;
- Handle texth;
- long text_size;
- short search_text;
- long cr_pos;
- short item;
- OSErr err;
- long sel_start,sel_end;
-
- item=GetSelectedItem(wt);
- if ((wt!=WT_None)&&(item>0)){
- enable_cut=HaveTextSelection(wt);
- offset=0;
- enable_paste=(GetScrap((Handle)0,'TEXT',&offset)>0);
- TextGetSelect(WindowInfo[wt].items[item]->data,&sel_start,&sel_end);
- TextGetSize(WindowInfo[wt].items[item]->data,&text_size);
- enable_select_all=((sel_start!=0)||(sel_end!=text_size));
- }
-
- // deal with the nasty paste problem, ie preventing them pasting CRs into single line fields
- if ((enable_paste)&&(WindowInfo[wt].items[item]->flags&(1<<tf_single_line))){
- texth=NewHandle(0);
- err=MemError();
- if (err==noErr){
- offset=0;
- text_size=GetScrap(texth,'TEXT',&offset);
- if (text_size>0){
- search_text=0x0d0d;
- cr_pos=Munger(texth,0,(Ptr)&search_text,1,(Ptr)0,0);
-
- if (cr_pos<0)
- enable_paste=true;
- else if (cr_pos==text_size-1)
- enable_paste=(cr_pos>0);
- else
- enable_paste=false;
- } else
- enable_paste=false;
- }
- if (texth!=(Handle)0)
- DisposeHandle((Handle)texth);
- }
-
- // deal with locked items
- if (item>0){
- if (IsLocked(wt,item)){
- enable_paste=false;
- enable_cut=false;
- }
- }
-
- // hit the menu items
- mh=GetMenuHandle(M_Edit);
- SetItemEnable(mh,EM_Undo,enable_undo);
- SetItemEnable(mh,EM_Cut,enable_cut);
- SetItemEnable(mh,EM_Copy,enable_cut);
- SetItemEnable(mh,EM_Paste,enable_paste);
- SetItemEnable(mh,EM_Clear,enable_cut);
- SetItemEnable(mh,EM_SelectAll,enable_select_all);
- }
-
- void JustSelectTextItem(WindowType wt,short item){
- if (GetSelectedItem(wt)>0)
- TextActivate(WindowInfo[wt].items[GetSelectedItem(wt)]->data,false);
- TextActivate(WindowInfo[wt].items[item]->data,((FrontWindow()==WindowInfo[wt].window)&&(InForeground())));
- WindowInfo[wt].selected_item=item;
- }
-
- void SelectTextItem(WindowType wt,short item){
- JustSelectTextItem(wt,item);
- TextSetSelect(WindowInfo[wt].items[item]->data,0,32767);
- }
-
- void DoTextMenu(WindowType wt,short menuitem){
- Ptr data;
- Handle texth;
- long offset,cr_pos,text_size;
- short search_text,item;
-
- item=GetSelectedItem(wt);
- data=WindowInfo[wt].items[item]->data;
-
- switch(menuitem){
- case EM_Undo:
- break;
- case EM_Cut:
- TextCut(data);
- break;
- case EM_Copy:
- TextCopy(data);
- break;
- case EM_Paste:
- if (IsLocked(wt,item))
- LockedAlert(wt,item);
- else {
- if (WindowInfo[wt].items[item]->flags&(1<<tf_single_line)){
- texth=NewHandle(0);
- if (texth!=(Handle)0){
- offset=0;
- text_size=GetScrap(texth,'TEXT',&offset);
- if (text_size>0){
- search_text=0x0d0d;
- cr_pos=Munger(texth,0,(Ptr)&search_text,1,(Ptr)0,0);
- if (cr_pos>0)
- SetHandleSize(texth,cr_pos);
- TextClear(data);
- TextInsert(data,texth);
- }
- DisposeHandle(texth);
- }
- } else {
- TextPaste(data);
- }
- }
- break;
- case EM_Clear:
- if (IsLocked(wt,item))
- LockedAlert(wt,item);
- else
- TextClear(data);
- break;
- case EM_SelectAll:
- TextSetSelect(data,0,32767);
- break;
- default:
- break;
- }
- }
-
- Boolean BlockCompare(Ptr lhs,Ptr rhs,long size){
- register long i=0;
- register unsigned long* ul,* ur;
- Boolean ret=false;
- long csize=size/sizeof(unsigned long);
- short left=size%sizeof(unsigned long); // leftover to compare...
-
- // do fast comparison on long values (compare 4 bytes at a time)
- ul=(unsigned long*)lhs;
- ur=(unsigned long*)rhs;
-
- while (i<csize){
- if (*ul!=*ur)
- return false;
- ul++;
- ur++;
- i++;
- }
-
- // if we get here, the major chunk of memory matched
- // the only thing left to check is any odd bytes trailing at the end of the blocks
- if (left){
- // odd size being compared
- register unsigned char* cl,*cr,ct;
-
- cl=(unsigned char*)ul;
- cr=(unsigned char*)ur;
-
- for (ct=0;ct<left;ct++){
- if (*cl!=*cr)
- return false;
- cl++;
- cr++;
- }
- }
-
- // everything checked out! return true
- return true;
- }
-
- void ScrambleHandle(Handle texth){
- register long l=0L,ct=GetHandleSize(texth);
- register unsigned char* up;
- unsigned char uc,val,val2,u4,u2,u3;
-
- up=(unsigned char*)*texth;
-
- while (l<ct){
- *up=(*up)^(0x55+(l+1));
-
- up++;
- l++;
- }
- }
-
- void BlockFill(Ptr p,long size,char value){
- union {
- unsigned long lval;
- char cval[4];
- } block;
- register long i=0;
- register unsigned long* ul;
- long csize=size/sizeof(unsigned long);
- short left=size%sizeof(unsigned long); // leftover to compare...
-
- // fill up the block
- block.cval[0]=block.cval[1]=block.cval[2]=block.cval[3]=value;
-
- // do fast set with long values (compare 4 bytes at a time)
- ul=(unsigned long*)p;
-
- while (i<csize){
- *ul=block.lval;
- ul++;
- i++;
- }
-
- // if we get here, the major chunk of memory has been set
- // the only thing left to do is any odd bytes trailing at the end of the blocks
- if (left){
- // odd size being compared
- register unsigned char* cl,ct;
-
- cl=(unsigned char*)ul;
-
- for (ct=0;ct<left;ct++){
- cl[ct]=value;
- }
- }
- }
-
- OSErr WhatOpenText(WindowType wt,short item){
- OSErr err;
- Str31 key;
- long attr;
- Handle texth;
- long junk,flags;
- Boolean pstring,scrambled;
- short font,size;
- Rect r;
-
- WindowInfo[wt].items[item]->data=(Ptr)0;
- WindowInfo[wt].items[item]->spare_data=(Ptr)0;
- texth=(Handle)0;
-
- SetPString(key,1,WindowInfo[wt].items[item]->key);
- flags=WindowInfo[wt].items[item]->flags;
- pstring=flags&(1<<tf_pstring);
- scrambled=flags&(1<<tf_scrambled);
-
- if (flags&(1<<tf_monospace)){
- font=monaco;
- size=9;
- } else {
- font=systemFont;
- size=12;
- }
-
- err=ICMapErr(ICGetPrefHandle(GetInstance(),key,&attr,&texth));
- if (err==noErr){
- ProcessAttributes(wt,item,attr);
- if (pstring)
- Munger(texth,0,(Ptr)0,1,(Ptr)&junk,0);// strip the first char
- if (scrambled)
- ScrambleHandle(texth);
- err=TextCreate(&(WindowInfo[wt].items[item]->data),WindowInfo[wt].window,item,font,
- size,IsLocked(wt,item));
- }
- if (err==noErr){
- SetDItemHandle(WindowInfo[wt].window,item,(Handle)gDrawTextProc);
- junk=GetHandleSize(texth);
- // (*texth)[junk]=0;
- TextInsert(WindowInfo[wt].items[item]->data,texth);
- }
-
- // build password dialog string
- if ((err==noErr)&&(scrambled)){
- WindowInfo[wt].items[item]->spare_data=WindowInfo[wt].items[item]->data;
- WindowInfo[wt].items[item]->data=(Ptr)0;
- GetDItemRect(WindowInfo[wt].window,item,&r);
- OffsetRect(&r,16000,0);
- TextMove(WindowInfo[wt].items[item]->spare_data,&r);
- BlockFill(*texth,GetHandleSize(texth),'•');
-
- err=TextCreate(&(WindowInfo[wt].items[item]->data),WindowInfo[wt].window,item,font,
- size,IsLocked(wt,item));
-
- if (err==noErr)
- TextInsert(WindowInfo[wt].items[item]->data,texth);
- }
-
- if (texth!=(Handle)0)
- DisposeHandle(texth);
-
- return err;
- }
-
- OSErr WhatFlushText(WindowType wt,short item){
- OSErr err;
- Str31 key;
- Handle texth=(Handle)0,oldtexth=(Handle)0;
- long attr,flags,texts,oldtexts;
- short i;
- Boolean pstring,scrambled;
-
- texth=oldtexth=(Handle)0;
- SetPString(key,1,WindowInfo[wt].items[item]->key);
- flags=WindowInfo[wt].items[item]->flags;
- pstring=flags&(1<<tf_pstring);
- scrambled=flags&(1<<tf_scrambled);
- err=ICMapErr(ICGetPrefHandle(GetInstance(),key,&attr,&oldtexth));
- if (err==noErr){
- if ((pstring)&&(GetHandleSize(oldtexth)==0)){
- // pref is non-existent or empty, turn oldtext into a handle for an empty pascal string
- SetHandleSize(oldtexth,1);
- err=MemError();
- if (err==noErr)
- **oldtexth=0; // terminate the empty pstring
- }
- }
- if (err==noErr){
- texth=NewHandle(0);
- err=MemError();
- }
-
- if (err==noErr){
- if (scrambled){
- TextGet(WindowInfo[wt].items[item]->spare_data,texth);
- ScrambleHandle(texth);
- } else
- TextGet(WindowInfo[wt].items[item]->data,texth);
- if (pstring){
- if (GetHandleSize(texth)>255)
- SetHandleSize(texth,255);
- i=GetHandleSize(texth)*0x0101; // puts it into both bytes!
- // insert the size byte into the handle at position zero
- Munger(texth,0L,(Ptr)0,0L,&i,1);// if this errors, we lose data
- }
- // should it go out to the prefs file?
- texts=GetHandleSize(texth);
- oldtexts=GetHandleSize(oldtexth);
- {
- Boolean islocked=IsLocked(wt,item);
- Boolean sameSize=(texts==oldtexts);
- Boolean sameText=true;
-
- if (sameSize) // only check the text if the sizes are the same
- sameText=BlockCompare(*texth,*oldtexth,texts);
-
- if (islocked){
- // can't update the doc, it is a locked field
- } else if (sameSize&&sameText){
- // nothing changed, so don't update the doc
- } else {
- // either the size or text is different, update the doc
-
- err=ICMapErr(ICSetPrefHandle(GetInstance(),key,ICattr_no_change,texth));
-
- if (err==noErr)
- DirtyDocument();
- }
- }
- }
- if (texth!=(Handle)0)
- DisposeHandle(texth);
- if (oldtexth!=(Handle)0)
- DisposeHandle(oldtexth);
-
- return err;
- }
-
- OSErr WhatCloseText(WindowType wt,short item){
- long flags;
- Boolean scrambled;
-
- flags=WindowInfo[wt].items[item]->flags;
- scrambled=flags&(1<<tf_scrambled);
- TextDestroy(&(WindowInfo[wt].items[item]->data));
- if (scrambled)
- TextDestroy(&(WindowInfo[wt].items[item]->spare_data));
- return noErr;
- }
-
- OSErr WhatClickText(WindowType wt,short item,EventRecord* er){
- JustSelectTextItem(wt,item);
- TextClick(WindowInfo[wt].items[item]->data,er);
- return noErr;
- }
-
- OSErr WhatKeyText(WindowType wt,short item,EventRecord* er){
- long flags;
- Boolean scrambled,single_line;
- char ch;
- long sel_start,sel_end;
-
- flags=WindowInfo[wt].items[item]->flags;
- scrambled=flags&(1<<tf_scrambled);
- single_line=flags&(1<<tf_single_line);
- ch=(er->message&charCodeMask)&0xff;
-
- // if a return on a single line or whitespace with scrambled
- if (((single_line)&&(ch==13))||((scrambled)&&(ch!=8)&&(ch<' ')))
- SysBeep(1);
- else {
- if ((IsLocked(wt,item))&&(DirtyKey(ch)))
- LockedAlert(wt,item);
- else {
- if (scrambled){
- TextGetSelect(WindowInfo[wt].items[item]->data,&sel_start,&sel_end);
- TextSetSelect(WindowInfo[wt].items[item]->spare_data,sel_start,sel_end);
- TextKey(WindowInfo[wt].items[item]->spare_data,er);
- if ((ch!=8)&&(DirtyKey(ch)))
- er->message='•';
- }
-
- TextKey(WindowInfo[wt].items[item]->data,er);
- }
- }
-
- return noErr;
- }
-